package com.rg1906.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.rg1906.entity.LoginUser;
import com.rg1906.entity.QuerygradeClass;
import com.rg1906.service.impl.StudentScoreServiceImpl;


@WebServlet("/StudentScore")
public class StudentScore extends HttpServlet {
	private static final long serialVersionUID = 1L;
	StudentScoreServiceImpl service=new StudentScoreServiceImpl();
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HashMap<String,Object> map = new HashMap<>();
		String likeName = request.getParameter("likeName");
		String likeClass = request.getParameter("likeClass");
		String pageNumber;
		String pageSize;
		 pageNumber = request.getParameter("pageNumber");
		 pageSize = request.getParameter("pageSize");
		if(pageNumber==null||pageSize==null) {
			pageNumber="0";
			pageSize="3";
		}
		HttpSession session = request.getSession();
		LoginUser user = (LoginUser)session.getAttribute("user");//获取session中的user.
		String loginName = user.getLoginName();//登录账号为学号或者老师的工作号
		int rid = user.getRid();//角色判断角色id
		String Gid=null;
		String Cid=null;
		Integer Cid3;
		if(loginName.startsWith("S")&&rid==4) {
			String[] split = loginName.split("S");
			String sNum=split[1];
			String[] split2 = sNum.split("000");
			 Gid = split2[0];//分割为年级id
			String Cid1 = split2[1];
			int Cid2 = Integer.parseInt(Cid1);
			if(Cid2<100) {
				 Cid3=Cid2/10;
			}else {
				Cid3=Cid2/100;
			}
			 Cid = Cid3.toString();//为班级id
			 
		}else if(loginName.startsWith("T")&&rid==3) {
			String[] split = loginName.split("T");
			String sNum=split[1];
			String[] split2 = sNum.split("000");
			 Gid = split2[0];//分割为年级id	 
		}
		
		try {
			List<QuerygradeClass> list1 = null;
			String gradename = service.queryGradeName(Gid);//年级
			if(Gid!=null&&Cid!=null) {
				 list1 = service.queryGradeClass(Gid,Cid);//学生账号查找到的班级
			}else if(Gid!=null&&Cid==null) {
				 list1 = service.queryGradeClass1(Gid);
			}
			String likeGradeName=null;
			String likeGradeClass=null;
			if(likeClass!=null&&!"".equals(likeClass)) {
				String[] likeClass1 = likeClass.split("级");
				likeGradeName = likeClass1[0]+"级";//模糊查询年级
				likeGradeClass=likeClass1[1];//模糊查询班级
			}
			Long total = service.GetCount(Gid,Cid,likeName,rid,likeGradeName,likeGradeClass);//查找总条数
			List<com.rg1906.entity.StudentScore> list = service.GetScore(pageNumber,pageSize,Gid,Cid,rid,likeName,likeGradeName,likeGradeClass);
			map.put("rows", list);
			map.put("total", total);
			map.put("gradename", gradename);
			map.put("list1", list1);
			response.getWriter().print(JSON.toJSONString(map));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
